﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net.Sockets;
using System.Windows;
using System.Runtime.InteropServices;

namespace Komunikator.Core
{
    /* 
     * Reader do socketa, bo nie ma wbudowanych
     */

    class SocketStreamReader
    {
        private Socket socket;
        private NetworkStream netStream;

        public SocketStreamReader(Socket sock)
        {
            socket = sock;
            netStream = new NetworkStream(socket);
            netStream.ReadTimeout = 100;
        }

        public SocketStreamReader(NetworkStream stream)
        {
            netStream = stream;
            netStream.ReadTimeout = 100;
        }

        public Int32 ReadInt()
        {
            byte[] buffer = new byte[4];
            try
            {
                
                netStream.Read(buffer, 0, 4);
                return BitConverter.ToInt32(buffer, 0);
            }
            catch (IOException e)
            {
                //MessageBox.Show("Read timeout");
                throw e;
                //return -1;
            }
        }

        public char ReadChar()
        {
            throw new NotImplementedException();
        }

        public byte ReadByte()
        {
            throw new NotImplementedException();
        }

        public short ReadShort()
        {
            throw new NotImplementedException();
        }

        public void WriteGGpacket(GGheader header)
        {
            byte[] buff = RawSerialize(header);
            netStream.Write(buff, 0, buff.Length);
        }

        public void WriteGGpacket(GGlogin80 loginPacket)
        {
            byte[] buff = RawSerialize(loginPacket);
            //for (int i = 0; i < buff.Length; i++)
            //{
            netStream.Write(buff, 0, buff.Length);
            //}
        }

        public void WriteGGpacket(List<byte> ggBytesList)
        {
            //byte[] buff = RawSerialize(loginPacket);
            //for (int i = 0; i < buff.Length; i++)
            //{
            netStream.Write(ggBytesList.ToArray(), 0, ggBytesList.ToArray().Length);
            //}
        }

        public void WriteGGpacket(byte[] ggBytes)
        {
            for (int i = 0; i < ggBytes.Length; i++)
            {
                //netStream.Write(ggBytes, 0, ggBytes.Length);
                netStream.WriteByte(ggBytes[i]);
            }
        }

        public static byte[] RawSerialize(object anything) //zamiana struct w bytes[] zeby przeslac przez siec
        {
            int rawsize = Marshal.SizeOf(anything);
            IntPtr buffer = Marshal.AllocHGlobal(rawsize);
            Marshal.StructureToPtr(anything, buffer, false);
            byte[] rawdatas = new byte[rawsize];
            Marshal.Copy(buffer, rawdatas, 0, rawsize);
            Marshal.FreeHGlobal(buffer);
            return rawdatas;
        } 

    }
}
